package com.kedsoft.andaily.service.impl;

import com.kedsoft.andaily.entity.Category;
import com.kedsoft.andaily.mapper.CategoryMapper;
import com.kedsoft.andaily.service.CategoryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kedsoft.andaily.vo.CategorySelectVo;
import com.kedsoft.andaily.vo.Pagination;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LGP
 * @since 2018-11-29
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    public Pagination<Category> pageList(Pagination<Category> pagination, Map<String, Object> query) {
        query.put("offset", pagination.offset());
        query.put("limit", pagination.limit());
        query.put("sort", pagination.getSort());
        query.put("order", pagination.getOrder());
        pagination.setItems(baseMapper.list(query));
        pagination.setTotal(baseMapper.count(query));
        return pagination;
    }

    @Override
    public List<CategorySelectVo> getAll() {
        List<Category> list = baseMapper.listAll();
        List<CategorySelectVo> result = new ArrayList<>();
        CategorySelectVo top = new CategorySelectVo();
        top.setId(0);
        top.setText("顶级栏目");
        result.add(top);
        if(list == null)
            return result;
        else{
            for(Category category : list){
                CategorySelectVo selectVo = new CategorySelectVo();
                selectVo.setId(category.getId());
                selectVo.setText(category.getTitle());
                result.add(selectVo);
            }
        }
        return result;
    }

    @Override
    public List<CategorySelectVo> getListAll() {
        List<Category> list = baseMapper.listAll();
        List<CategorySelectVo> result = new ArrayList<>();
        CategorySelectVo top = new CategorySelectVo();
        //result.add(top);
        if(list == null)
            return result;
        else{
            for(Category category : list){
                CategorySelectVo selectVo = new CategorySelectVo();
                selectVo.setId(category.getId());
                selectVo.setText(category.getTitle());
                result.add(selectVo);
            }
        }
        return result;
    }

    @Override
    public List<Category> getList() {
        List<Category> list = baseMapper.listAll();
        return list;
    }
}
